package com.zy.cat.controller.game.v1;

import java.util.List;
import java.util.concurrent.Callable;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zy.cat.common.annotation.CheckDevPermission;
import com.zy.cat.common.annotation.CheckingMD5;
import com.zy.cat.dao.Dao;
import com.zy.cat.entity.game.CardDrawCountHistory;
import com.zy.cat.entity.game.CardDrawEntryInfo;
import com.zy.cat.entity.game.CardDrawHistory;
import com.zy.cat.entity.game.CardPokerDrawHistory;
import com.zy.cat.entity.game.repository.CardDrawCountHistoryRepository;
import com.zy.cat.entity.game.repository.CardDrawHistoryRepository;
import com.zy.cat.entity.game.repository.CardPokerDrawHistoryRepository;
import com.zy.cat.entity.game.resp.CardDrawHistoryResp;
import com.zy.cat.entity.game.resp.CardInfoResp;
import com.zy.cat.entity.game.resp.CardPokerDrawHistoryResp;
import com.zy.cat.entity.game.resp.ScrollBarInfoResp;
import com.zy.cat.entity.resp.RSPE;
import com.zy.cat.service.CacheService;
import com.zy.cat.service.CatGameService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "9catGameCard", tags = { "小游戏-刮刮卡-开放API" }, description = "小游戏-刮刮卡-开放接口")
@RestController

@RequestMapping("/v1/game/card")
public class CatGameCardController {

	@Autowired
	CacheService cacheService;

	@Autowired
	CatGameService catGameService;
	
	@Autowired
	Dao<CardInfoResp> cardInfoRespDao;
	
	@Autowired
	Dao<CardDrawEntryInfo> cardDrawEntryInfoDao;
	
	@Autowired
	Dao<CardDrawCountHistory> countHistoryDao;
	
	@Autowired
	CardDrawCountHistoryRepository cardDrawCountHistoryRepository;
	
	@Autowired
	CardPokerDrawHistoryRepository cardPokerDrawHistoryRepository;
	
	@Autowired
	CardDrawHistoryRepository cardDrawCountHistory;
 

	@PostMapping("/info")
	@ApiOperation(value = "刮刮卡信息", notes = "每次进入请求，返回游戏进度等信息", consumes = "application/x-www-form-urlencoded", position = 1, response = CardInfoResp.class, responseContainer = "Map")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_pid_rtype_utoken_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "uuid_pid_rtype_utoken_uuid", MD5 = "token")
	@CheckDevPermission
	public Object getCardInfo(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		Callable<RSPE<CardInfoResp>> result = new Callable<RSPE<CardInfoResp>>() {
			@Override
			public RSPE<CardInfoResp> call() throws Exception {
				CardInfoResp result = catGameService.getCardInfo(rtype, pid);
				if (result == null) {
					return new RSPE<CardInfoResp>(-3, "获取信息失败！请稍后重试！");
				}
				return new RSPE<CardInfoResp>(result);
			}
		};
		return result;
	}
	
	@PostMapping("/draw")
	@ApiOperation(value = "刮刮卡进行抽奖", notes = "开始刮刮卡，返回中奖人头数和中奖现金豆金额", consumes = "application/x-www-form-urlencoded", position = 2, response = CardDrawHistoryResp.class, responseContainer = "List")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "code", value = "刮刮卡banner的code", example = "1001", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "gtype", value = "刮奖类型(1.1元、2.2元、3.5元、4.10元、5.20元、6.50元、7.80元、8.100元、9.200元、10.500元、11.1000元、12.1500元)", example = "1", dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_pid_utoken_code_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "uuid_pid_utoken_code_rtype_uuid", MD5 = "token")
	@CheckDevPermission
	public Object getCardDraw(Integer rtype, Integer gtype,String pid, String utoken,String code, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if (gtype == null || gtype<1||gtype>12 ) {
			return new RSPE<Object>(-3, "刮奖类型参数错误！");
		}
		/* 1.判断刮奖次数是否用尽 */
		CardDrawCountHistory countHistory=countHistoryDao.selectOne(new CardDrawCountHistory(pid), "getLastCards");
		if (countHistory!=null && countHistory.getDcount()<1 && gtype!=12) {
			return new RSPE<Object>(-4, "当前刮刮卡数量已不足！");
		}
		/*3. 检验当前banner是否已经抽取完*/
		CardDrawEntryInfo drawEntryInfo = cardDrawEntryInfoDao.selectOne(new CardDrawEntryInfo(pid,code), "getCardBannerInfo");
		if(drawEntryInfo.getStatus()==0) {
			return new RSPE<Object>(-8, "此刮刮卡次数已用完！");
		}
		
		Callable<RSPE<CardDrawHistoryResp>> result = new Callable<RSPE<CardDrawHistoryResp>>() {
			@Override
			public RSPE<CardDrawHistoryResp> call() throws Exception {
				return new RSPE<CardDrawHistoryResp>(catGameService.getCardDraw(rtype, pid,code,gtype));
			}
		};
		return result;
	}

	@PostMapping("/draw/double")
	@ApiOperation(value = "刮刮卡领取奖励或翻倍", notes = "观看广告，领取奖励或翻倍 ！", consumes = "application/x-www-form-urlencoded", position = 3, response = CardDrawHistoryResp.class, responseContainer = "Map")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "fid", value = "中奖翻倍ID", example = "1", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_pid_utoken_fid_pid_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	
	@CheckingMD5(checkingStr = "uuid_pid_utoken_fid_pid_rtype_uuid", MD5 = "token",checkSingle = true)
	@CheckDevPermission
	public Object getCardDrawDouble(Integer rtype,String fid,  String pid , String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if (StringUtils.isBlank(fid)||fid.length()!=32) {
			return new RSPE<Object>(-3, "中奖翻倍ID参数错误！");
		}
		
		/* 1.判断翻倍是否存在 */
		/* 2.判断是否已经翻倍 */
		CardDrawHistory drawHistory = cardDrawCountHistory.findByPidAndFid(pid, fid);
		if (drawHistory == null) {
			return new RSPE<Object>(-4, "翻倍不存在!");
		}
		if (drawHistory.getFtype()==1) {
			return new RSPE<Object>(-5, "已经翻倍，请不要重复请求！");
		}
		if (drawHistory.getFtype()==-1 && drawHistory.getDstatus()==1) {
			return new RSPE<Object>(-6, "奖励已领取,请勿重复领取！");
		}
		Callable<RSPE<CardDrawHistoryResp>> result = new Callable<RSPE<CardDrawHistoryResp>>() {
			@Override
			public RSPE<CardDrawHistoryResp> call() throws Exception {
				return new RSPE<CardDrawHistoryResp>(catGameService.getCardDrawDouble(rtype,drawHistory));
			}
		};
		return result;
	}
	
	@PostMapping("/poker/draw")
	@ApiOperation(value = "刮刮卡进行翻牌游戏奖", notes = "开始刮刮卡，返回剩余次数和中奖现金豆金额", consumes = "application/x-www-form-urlencoded", position = 4, response = CardPokerDrawHistoryResp.class, responseContainer = "List")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
 			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_pid_utoken_pid_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	
	@CheckingMD5(checkingStr = "uuid_pid_utoken_pid_rtype_uuid", MD5 = "token")
	@CheckDevPermission
	public Object getCardPokerDraw(Integer rtype ,String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		 int lastCount=cacheService.getCardPokerCount(pid);
		 if(lastCount<1) {
			 return new RSPE<Object>(-3, "今日次数已用尽！请明天重试！");
		 }
		Callable<RSPE<CardPokerDrawHistoryResp>> result = new Callable<RSPE<CardPokerDrawHistoryResp>>() {
			@Override
			public RSPE<CardPokerDrawHistoryResp> call() throws Exception {
				return new RSPE<CardPokerDrawHistoryResp>(catGameService.getCardPokerDraw(rtype, pid));
			}
		};
		return result;
	}

	@PostMapping("/poker/draw/double")
	@ApiOperation(value = "刮刮卡翻牌游戏进行抽奖翻倍", notes = "观看广告，进行奖励翻倍 ！", consumes = "application/x-www-form-urlencoded", position = 5, response = CardPokerDrawHistoryResp.class, responseContainer = "Map")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "fid", value = "中奖翻倍ID", example = "1", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_pid_utoken_fid_pid_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	
	@CheckingMD5(checkingStr = "uuid_pid_utoken_fid_pid_rtype_uuid", MD5 = "token")
	@CheckDevPermission
	public Object getCardPokerDrawDouble(Integer rtype,String fid,  String pid , String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if (StringUtils.isBlank(fid)||fid.length()!=32) {
			return new RSPE<Object>(-3, "中奖翻倍ID参数错误！");
		}
		
		/* 1.判断翻倍是否存在 */
		CardPokerDrawHistory poker=cardPokerDrawHistoryRepository.findByPidAndFid(pid, fid);
		if(poker==null) {
		 return new RSPE<Object>(-4, "翻倍信息不存在!");
		}
		
		/* 2.判断是否已经翻倍 */	
		if(poker.getFtype()!=0) {
		 return new RSPE<Object>(-5, "已经翻倍，请不要重复请求！");
		}
		Callable<RSPE<CardPokerDrawHistoryResp>> result = new Callable<RSPE<CardPokerDrawHistoryResp>>() {
			@Override
			public RSPE<CardPokerDrawHistoryResp> call() throws Exception {
				return new RSPE<CardPokerDrawHistoryResp>(catGameService.getCardPokerDrawDouble(rtype,poker));
			}
		};
		return result;
	}
	
	@PostMapping("/scroll/bar/list")
	@ApiOperation(value = "刮刮卡滚动条展示内容", notes = "刮刮卡滚动条展示内容 ！", consumes = "application/x-www-form-urlencoded", position = 6, response = ScrollBarInfoResp.class, responseContainer = "List")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_utoken_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	
	@CheckingMD5(checkingStr = "uuid_utoken_rtype_uuid", MD5 = "token")
	@CheckDevPermission
	public Object getCardScrollBarList(Integer rtype, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		
		Callable<RSPE<List<ScrollBarInfoResp>>> result = new Callable<RSPE<List<ScrollBarInfoResp>>>() {
			@Override
			public RSPE<List<ScrollBarInfoResp>> call() throws Exception {
				return new RSPE<List<ScrollBarInfoResp>>(catGameService.getCardScrollBarList(rtype));
			}
		};
		return result;
	}
}
